﻿package com.gambit.happilyeverafter.levels {
	import com.gambit.happilyeverafter.characters.BaseCharacter;
	import com.gambit.happilyeverafter.characters.Knight;
	import com.gambit.happilyeverafter.characters.Ninja;
	import com.gambit.happilyeverafter.characters.Warrior;
	import org.flixel.FlxLayer;
	import org.flixel.FlxSprite;
	import org.flixel.FlxState;
	import org.flixel.FlxTilemap;
	import org.flixel.FlxG;
	
	/**
	 * ...
	 * @author William Hutama
	 */
	public class BaseLevel extends FlxState {
		[Embed(source = "../../../../data/levels/fairy_blks.png")]
		private var MapTiles:Class;
		
		[Embed(source = "../../../../data/levels/map0.txt", mimeType = "application/octet-stream")]
		private var DataMap:Class;
		
		[Embed(source = "../../../../data/bg.png")]
		protected var BackgroundImageClass:Class;
		
		private var _layer_bg:FlxLayer;
		private var _layer_stage:FlxLayer;
		private var _layer_sprites:FlxLayer;
		
		protected var _bg:FlxSprite;
		private var _map:FlxTilemap;
		
		protected var _player_warrior:BaseCharacter;
		protected var _player_knight:BaseCharacter;
		protected var _player_ninja:BaseCharacter;
		
		protected var _players:Array = new Array();
		
		private var _active_player_index:int;
		
		public function BaseLevel() {
			super();
			
			_layer_bg = new FlxLayer();
			_layer_stage = new FlxLayer();
			_layer_sprites = new FlxLayer();
			
			_bg = new FlxSprite(32, 32, BackgroundImageClass);
			//_bg.scale.x = _bg.scale.y = 2;
			//_bg.alpha = 0.6;
			_bg.scrollFactor.x = 0.25;
			_bg.scrollFactor.y = 0.25;
			_layer_bg.add(_bg);
			
			_map = new FlxTilemap();
			_map.loadMap(new DataMap(), MapTiles, 32, 32);
			_map.drawIndex = 1;
			_map.collideIndex = 1;
			
			_layer_stage.add(_map);
			
			_player_warrior = new  Warrior(64, 464);
			_player_knight = new Knight(100, 450);
			_player_ninja = new Ninja(150, 440);
			_players.push(_player_warrior);
			_players.push(_player_knight);
			_players.push(_player_ninja);
			
			_layer_sprites.add(_player_warrior);
			_layer_sprites.add(_player_knight);
			_layer_sprites.add(_player_ninja);
			
			FlxG.follow(_player_warrior, 2.5);
			
			this.add(_layer_bg);
			this.add(_layer_stage);
			this.add(_layer_sprites);
		}
		
		override public function update():void {
			super.update();
			
			_map.collideArray(_players);
			FlxG.overlapArrays(_players, _players, onPlayersCollide);
			
			
			if (FlxG.keys.justPressed("Z")) {
				switch_active_player();
			}
		}
		
		private function switch_active_player():void {
			_active_player_index++;
			if (_active_player_index >= _players.length) {
				_active_player_index = 0;
			}
			FlxG.follow(_players[_active_player_index], 2.5);
		}
		private function onPlayersCollide(p1:FlxSprite, p2:FlxSprite):void {
			if (p1.y < p2.y) {
				p1.velocity.y = -200;
				p2.velocity.y = 0;
			}
			else if (p1.y > p2.y) {
				p1.velocity.y = 0;
				p2.velocity.y = -200;
			}
			
		}
	}

}